<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        #title {
            line-height: 40px;
        }

        .list li {
            line-height: 32px;
            border: 1px dashed #333;
        }

        .list li+li {
            border-top: 0;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <h3 id="title">dom操作封装</h3>
        <ul class="list">
            <li>01</li>
            <li>02</li>
            <li>03</li>
            <li>04</li>
            <li>05</li>
            <li>06</li>
        </ul>
    </div>
</body>
<script>
    // dom操作
    // 简单的来说 对文档(html页面)上的元素进行增删改查 还有获取元素,修改元素的内容, 属性, 样式 ,绑定事件


    // DOM操作封装 => 按功能封装

    // 面向对象
    // 字面量对象
    /* var getEle = {
        list: document.querySelectorAll(".list li"),
        getText: function () {
            // this -> getEle
            return this.list[0].textContent;
        },
        setText: function (con) {
            // this -> getEle
            for (var i = 0; i < this.list.length; i++) {
                var ele = this.list[i];
                ele.textContent = con;
            }
        }
    }

    getEle.setText(33333); */

    // selector 传入一个css选择器 => 根据传入的选择器获取元素
    /* function getEle(selector) {
        var obj = {
            list: document.querySelectorAll(selector),
            getText: function () {
                // this -> getEle
                return this.list[0].textContent;
            },
            setText: function (con) {
                // this -> getEle
                for (var i = 0; i < this.list.length; i++) {
                    var ele = this.list[i];
                    ele.textContent = con;
                }
            }
        }
        return obj;
    }


    var res1 = getEle(".list li");
    res1.setText("33333");
    var res2 = getEle("#title");
    res2.setText("44444"); */


    // 构造函数

    function GetEle(selector) {
        // {} 
        // {}[[Prototype]] = GetEle.prototype;
        // this -> {}

        // 把获取的元素集合存储到实例化对象中
        this.list = document.querySelectorAll(selector);  // {}.list=[li,li,li,li]

        // return this;  // {list:[li,li,li,li]}
    }

    GetEle.prototype.getText = function () {
        // this -> 调用此方法的实例化对象    {list:[li,li,li,li]}
        // this.list  -> 通过实例化对象获取元素的集合
        return this.list[0].textContent;
    }
    GetEle.prototype.setText = function (con) {
        // this -> 调用此方法的实例化对象    {list:[li,li,li,li]}
        for (var i = 0; i < this.list.length; i++) {
            var ele = this.list[i];
            ele.textContent = con;
        }
    }

    var res1 = new GetEle(".list li");
    res1.setText("33333");

    var res2 = new GetEle("#title");
    res2.setText("44444");
















</script>

</html>